package com.eshop;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class editCustomer
 */
@WebServlet("/EditCustomer")
public class EditCustomer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditCustomer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//write UI
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
		out.println("<html>");
		out.println("<head>");
		out.println("  <title>EShop</title>");
		out.println("  <style type=\"text/css\">");
		out.println("  body { font-family: Georgia, \"Times New Roman\",Times, serif; color: white; background-color: #272e34; background-image:url('images/background.jpg'); background-repeat:no-repeat;}");
		out.println("  h1.title   {margin-bottom:0px; margin-top:0px; font-family: Chiller, Geneva, Arial,SunSans-Regular, sans-serif; font-size:200px; color: #45c5f2; position: relative; left:500px;}");
		out.println("  table.navbar { color: #45c5f2;left:600px; position:relative;}");
		out.println("  table.navbar td {  border:2px; border-style:solid; border-color:#45c5f2 #45c5f2;}");
		out.println("  table.registerForm{ color: #45c5f2; left:600px;  top:30px; position:relative;}");
		out.println("  a:link 	{ text-decoration:  none; color: #45c5f2  }");
		out.println("  a:visited { text-decoration:  none; color: #45c5f2  }");
		out.println(" </style>");
		out.println("</head>");
		out.println("<body>");
		
		out.println(" <h1 class=\"title\">EShop</h1>");
		out.println("<!-- Site navigation menu -->");
		out.println("<table class=\"navbar\">");
		out.println(" <td><a href=\"index.html\">Home page</a></td>");
		out.println(" <td><a href=\"Products.html\">Products</a></td>");
		out.println(" <td><a href=\"ShoppingCart.html\">Shopping Cart</a></td>");
		out.println(" <td><a href=\"Register.html\">Register</a></td>");
		out.println(" </table>");
		
		String userName = request.getParameter("username");
		String firstName = request.getParameter("FirstName");
		
		String secondName = request.getParameter("SecondName");
		String Address = request.getParameter("Address");
		String Phone = request.getParameter("Phone");
		
		out.println("<form class=\"register\" method=\"Post\" action=\"EditCustomer.html\">");
		out.println("<table class=\"registerForm\" border=\"0\">");
		out.println("<tr><td>Username: <input type=\"text\" name=\"username\" value=\"" + userName + "\"  size=\"20\"></td></tr>");
		out.println("<tr><td>First Name: <input type=\"text\" name=\"firstname\" value=\"" + firstName + "\"  size=\"20\"></td></tr>");
		out.println("<tr><td>Second Name: <input type=\"text\" name=\"secondname\" value=\"" + secondName + "\" size=\"20\"></td></tr>");
		out.println("<tr><td>Address: <input type=\"text\" name=\"address\" value=\"" + Address + "\" size=\"20\"></td></tr>");
		out.println("<tr><td>Phone:<input type=\"text\" name=\"phone\" value=\"" + Phone + "\" size=\"20\"></td></tr>");
		out.println("<tr><td><input type=\"submit\" value=\"Submit\" name=\"B1\"></td></tr>");	
		out.println("</table>");
		out.println("</form>");
		
		out.println("</BODY></HTML>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection 			connection 	= null;
		Statement 			stmt 		= null;
		PreparedStatement 	pst 		= null;
				
		ResultSet rs = null;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String usrname 	= request.getParameter("username");
		String fname 	= request.getParameter("firstname");
		String sname 	= request.getParameter("secondname");
		String address 	= request.getParameter("address");
		String phone 	= request.getParameter("phone");
		
		try{
			// Load (and therefore register) the Oracle Driver
			Class.forName("com.mysql.jdbc.Driver");
			// Get a Connection to the database
			connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/eShop", "root", "admin");
		
			
			if(fname == "" || sname == "" || address == "" || phone == "")
			{
				// Display the result set as a list
				//write UI
				out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
				out.println("<html>");
				out.println("<head>");
				out.println("  <title>EShop</title>");
				out.println("  <style type=\"text/css\">");
				out.println("  body { font-family: Georgia, \"Times New Roman\",Times, serif; color: white; background-color: #272e34; background-image:url('images/background.jpg'); background-repeat:no-repeat;}");
				out.println("  h1.title   {margin-bottom:0px; margin-top:0px; font-family: Chiller, Geneva, Arial,SunSans-Regular, sans-serif; font-size:200px; color: #45c5f2; position: relative; left:500px;}");
				out.println("  table.navbar { color: #45c5f2;left:600px; position:relative;}");
				out.println("  table.navbar td {  border:2px; border-style:solid; border-color:#45c5f2 #45c5f2;}");
				out.println("  table.registerForm{ color: #45c5f2; left:600px;  top:30px; position:relative;}");
				out.println("  a:link 	{ text-decoration:  none; color: #45c5f2  }");
				out.println("  a:visited { text-decoration:  none; color: #45c5f2  }");
				out.println(" </style>");
				out.println("</head>");
				out.println("<body>");
				
				out.println(" <h1 class=\"title\">EShop</h1>");
				out.println("<!-- Site navigation menu -->");
				out.println("<table class=\"navbar\">");
				out.println(" <td><a href=\"index.html\">Home page</a></td>");
				out.println(" <td><a href=\"Products.html\">Products</a></td>");
				out.println(" <td><a href=\"ShoppingCart.html\">Shopping Cart</a></td>");
				out.println(" <td><a href=\"Register.html\">Register</a></td>");
				out.println(" </table>");
				
				out.println("<form class=\"register\" method=\"Post\" action=\"EditCustomer.html\">");
				out.println("<table class=\"registerForm\" border=\"0\">");
				if(usrname == "")
					out.println("<tr><td>Username: <input 	type=\"text\" " 
								+ "name=\"username\" "
								+ "STYLE=\"color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;\""
								+ "size=\"20\"></td></tr>");
				else
					out.println("<tr><td>Username: <input type=\"text\" " 
								+ "name=\"username\" " 
								+ "size=\"20\""
								+ "value = " + usrname + ">"
								+ "</td></tr>");
				
				if(fname == "")
					out.println("<tr><td>First Name: <input 	type=\"text\" " 
								+ "name=\"firstname\" "
								+ "STYLE=\"color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;\""
								+ "size=\"20\">/td></tr>");
				else
					out.println("<tr><td>First Name: <input type=\"text\" " 
								+ "name=\"firstname\" " 
								+ "size=\"20\""
								+ "value = " + fname + ">"
								+ "/td></tr>");
				
				if(sname == "")
					out.println("<tr><td>Second Name: <input 	type=\"text\" " 
								+ "name=\"secondname\" "
								+ "STYLE=\"color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;\""
								+ "size=\"20\">/td></tr>");
				else
					out.println("<tr><td>Second Name: <input type=\"text\" " 
								+ "name=\"secondname\" " 
								+ "size=\"20\""
								+ "value = " + sname + ">"
								+ "/td></tr>");
				
				if(address == "")
					out.println("<tr><td>Address: <input 	type=\"text\" " 
								+ "name=\"address\" "
								+ "STYLE=\"color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;\""
								+ "size=\"20\">/td></tr>");
				else
					out.println("<tr><td>Address: <input type=\"text\" " 
								+ "name=\"address\" " 
								+ "size=\"20\""
								+ "value = " + address + ">"
								+ "/td></tr>");
				
				if(phone == "")
					out.println("<tr><td>Phone: <input 	type=\"text\" " 
								+ "name=\"phone\" "
								+ "STYLE=\"color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;\""
								+ "size=\"20\">/td></tr>");
				else
					out.println("<tr><td>Phone: <input type=\"text\" " 
								+ "name=\"phone\" " 
								+ "size=\"20\""
								+ "value = " + phone + ">"
								+ "/td></tr>");
				
				out.println("<tr><td>  <input type=\"submit\" value=\"Submit\" name=\"B1\"></td></tr>");	
				out.println("</table>");
				out.println("</form>");
				
				out.println("</BODY></HTML>");
			}
			else
			{
				pst = (PreparedStatement) connection.prepareStatement("update  customer set FirstName = '"+ fname+"' ,  SecondName ='"+ sname+"' , Phone = '"+ phone+"', Address = '"+address+"' where username = " + usrname);
				pst = (PreparedStatement) connection.prepareStatement("update   customer set  FirstName=?, SecondName=?, Phone=?, Address =? where username= ? ");
				pst.setString(1, fname);
				pst.setString(2, sname);
				pst.setString(3, phone);
				pst.setString(4, address);
				pst.setString(5, usrname);
				
				int numRowsChanged = pst.executeUpdate();
				pst.close();
				
				// show that the new account has been created
				//write UI
				out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
				out.println("<html>");
				out.println("<head>");
				out.println("  <title>EShop</title>");
				out.println("  <style type=\"text/css\">");
				out.println("  body { font-family: Georgia, \"Times New Roman\",Times, serif; color: white; background-color: #272e34; background-image:url('images/background.jpg'); background-repeat:no-repeat;}");
				out.println("  h1.title   {margin-bottom:0px; margin-top:0px; font-family: Chiller, Geneva, Arial,SunSans-Regular, sans-serif; font-size:200px; color: #45c5f2; position: relative; left:500px;}");
				out.println("  h1.salute  {margin-bottom:0px; margin-top:0px; font-family: Georgia, Geneva, Arial,SunSans-Regular, sans-serif; font-size:50px; color: #45c5f2; position: relative; left:500px;}");
				out.println("  table.navbar { color: #45c5f2;left:600px; position:relative;}");
				out.println("  table.navbar td {  border:2px; border-style:solid; border-color:#45c5f2 #45c5f2;}");
				out.println("  table.productList{ color: #45c5f2; left:600px;  top:30px; position:relative;}");
				out.println("  a:link 	{ text-decoration:  none; color: #45c5f2  }");
				out.println("  a:visited { text-decoration:  none; color: #45c5f2  }");
				out.println(" </style>");
				out.println("</head>");
				out.println("<body>");
				
				out.println(" <h1 class=\"title\">EShop</h1>");
				
				out.println("<!-- Site navigation menu -->");
				out.println("<table class=\"navbar\">");
				out.println(" <td><a href=\"index.html\">Home page</a></td>");
				out.println(" <td><a href=\"Products.html\">Products</a></td>");
				out.println(" <td><a href=\"ShoppingCart.html\">Shopping Cart</a></td>");
				out.println(" <td><a href=\"Register.html\">Register</a></td>");
				out.println(" </table>");
				
				out.println(" <h1 class=\"salute\">Hello " + fname +"</h1>");
				out.println("</BODY></HTML>");
				
			}
		}
		catch(ClassNotFoundException e) {
			out.println("Couldn't load database driver: " + e.getMessage());
		}
		catch(SQLException e) {
			out.println("SQLException caught: " + e.getMessage());
		}
		finally {
			// Always close the database connection.
		try {
		if (connection != null) connection.close();
		}
		catch (SQLException ignored) { }
		}
	
	}

}
